//
// Created by tongj on 2023/11/29.
//

#include <base/lock.hpp>
#include <thread>

namespace tbs {
namespace lock {
bool MutexLock::lock(CONST int &sign) {
  _mx.lock();
  _is_lock = true;
  return true;
}

bool MutexLock::unlock(CONST int &sign) {
  _mx.unlock();
  _is_lock = false;
  return true;
}

bool MutexLock::isLocked(CONST int &sign) CONST {
  return _is_lock;
}

bool SpinLock::lock(CONST int &sign) {
  while (isLocked(sign)) {
	std::this_thread::yield();
  }
  _has_lock = true;
  return true;
}

bool SpinLock::unlock(CONST int &sign) {
  _has_lock = false;
  return true;
}

bool SpinLock::isLocked(CONST int &sign) CONST {
  return _has_lock;
}
}
}
